﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Controllers
{
    public class UserCompanyController : CommonController
    {
        //
        // GET: /Company/

        public ActionResult Index(string search)
        {
            var data = EdexContext
                .GetAllowedCompanies()
                .Select(r => r.COMPANY);
            return View(data);
        }

        public ActionResult Details(Guid id)
        {
            var data = EdexContext
                .GetAllowedCompanies()
                .Where(r => r.COMPANY_GUID == id)
                .Select(r => r.COMPANY)
                .SingleOrDefault();

            return View(data);
        }

        public ActionResult DetailsNew()
        {
            return View();
        }

        public ActionResult Save(EdexApiClient.WH_COMPANIES record)
        {
            if (record.GUID_RECORD == null || record.GUID_RECORD == Guid.Empty)
                EdexContext.AddCompany(record);
            else
                EdexContext.UpdateCompany(record);

            return Content("OK");
        }

        public ActionResult Delete(string id)
        {
            EdexContext.DeleteCompany(id);
            return Content("OK");
        }

        [HttpGet]
        public ActionResult AccessToUser()
        {
            return View();
        }

        [HttpPost]
        public ActionResult AccessToUser(string email)
        {
            var company_key = ((EdexApiClient.API_SESSION)Session["api_session"]).COMPANY_GUID.ToString();
            EdexContext.AccessToUser(email, company_key);

            return View("AccessToUserComplete");
        }
    }
}
